package com.css.apps.base.log.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.dict.service.DictMan;
import com.css.core.configuration.Environment;
import com.css.db.query.QueryCache;
import com.css.util.Ajax;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;

@SuppressWarnings("serial")
public class LogStatChart extends AdminAction {
	
	private static Log log = LogFactory.getLog(LogStatChart.class);
	private String statBeginDate;
	private String eventType;
	private String list0s, list1s, list2s, list3s, list4s, list10s, list20s, list21s;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	protected String adminGo() {
		try {
			if (StringHelper.isEmpty(statBeginDate)) {
				statBeginDate = new SimpleDateFormat("yyyy-MM").format(new Date());
			}
			if (StringHelper.isEmpty(eventType)) {
				eventType = "11";
			}
			
//			chart0
			String[] s = statBeginDate.split("-");
			Integer year1 = Integer.valueOf(s[0]);
			Integer month1 = Integer.valueOf(s[1]);
			List<Object[]> list = new ArrayList<Object[]>();
			doSearch(list, year1, month1);
			List list0 = new ArrayList();
			List list1 = new ArrayList();
			List list2 = new ArrayList();
			List list3 = new ArrayList();
			List list4 = new ArrayList();
			if(list!=null && list.size() != 0) {
				Object[] a = (Object[]) list.get(0);
				List<Object[]> tmpl = (List<Object[]>) a[1];
				for(Object[] b : tmpl) {
					list0.add(b[0]);
					list1.add(b[1]);
					list2.add(b[2]);
					list3.add(b[3]);
					list4.add(b[4]);
				}
			}
//			序列化数据
			StringBuilder sb = new StringBuilder();
			for(int i=0; i<list0.size();i++){
				sb.append((i==0?"":",")+"'"+DictMan.getDictType("d_eventtype",list0.get(i).toString()).getName()+"'");
			}
			list0s = sb.toString();
			
			sb = new StringBuilder();
			for(int i=0; i<list0.size();i++){
				sb.append((i==0?"":",")+(list1.get(i).toString().equals("0")?"NaN":list1.get(i).toString()));
			}
			list1s = sb.toString();
			
			sb = new StringBuilder();
			for(int i=0; i<list2.size();i++){
				sb.append((i==0?"":",")+(list2.get(i).toString().equals("0")?"NaN":list2.get(i).toString()));
        	}
			list2s = sb.toString();
			
			sb = new StringBuilder();
			for(int i=0; i<list3.size();i++){
				sb.append((i==0?"":",")+(list3.get(i).toString().equals("0")?"NaN":list3.get(i).toString()));
        	}
			list3s = sb.toString();
			
			
			
			
//			chart1
			list = new ArrayList<Object[]>();
			doSearch1(list, year1, eventType);
			List list10 = new ArrayList();
			for(int i=0; i<12; i++) {
				list10.add(0);
			}
			if(list!=null && list.size() != 0) {
				Object[] a = (Object[]) list.get(0);
				List<Object[]> tmpl = (List<Object[]>) a[1];
				for(Object[] b : tmpl) {
					list10.set(Integer.parseInt(b[0].toString())-1, Integer.parseInt(b[1].toString()));
				}
			}
//			序列化数据
			sb = new StringBuilder();
			for(int i=0; i<list10.size();i++){
				sb.append((i==0?"":",")+list10.get(i).toString());
			}
			list10s = sb.toString();
			
			
			
			
			
//			chart2
			list = new ArrayList<Object[]>();
			doSearch2(list, year1, month1);
			List list20 = new ArrayList();
			List list21 = new ArrayList();
			if(list!=null && list.size() != 0) {
				Object[] a = (Object[]) list.get(0);
				List<Object[]> tmpl = (List<Object[]>) a[1];
				for(Object[] b : tmpl) {
					list20.add(b[0]);
					list21.add(b[1]);
				}
			}
//			序列化数据
			sb = new StringBuilder();
			for(int i=0; i<list20.size();i++){
				sb.append((i==0?"":",")+"'"+DictMan.getDictType("d_eventtype",list20.get(i).toString()).getName()+"'");
        	}
			list20s = sb.toString();
	        sb = new StringBuilder();
	        for(int i=0; i<list21.size();i++){
	        	sb.append((i==0?"":",")+"{value:"+list21.get(i).toString()+",name:'"+DictMan.getDictType("d_eventtype",list20.get(i).toString()).getName()+"'}");
	    	}
	        list21s = sb.toString();
			
			
	        
	        
			return Action.SUCCESS;

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			setMessage(Messages.getString("systemMsg.exception"));
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages
					.getString("systemMsg.exception"));
			return Action.ERROR;
		}
	}

	public void doSearch(List<Object[]> list, Integer i, Integer j) {
		StringBuffer sb = new StringBuffer(
				" select a.event_type,sum(case when(a.log_level='1') then 1 else 0 end) as yiban,"
						+ " sum(case when(a.log_level='2') then 1 else 0 end) as yanzhong,"
						+ " sum(case when(a.log_level='3') then 1 else 0 end) as zhongyao,"
						+ " count(*) as zongji "
						+ " from s_log a " + getWhere(i, j));
		QueryCache qc = new QueryCache(sb.toString(), true);
		List<?> list1 = qc.listCache();
		if (list1 != null && list1.size() > 0) {
			Object[] obj3 = new Object[2];
			obj3[0] = "" + i + "-" + (j > 9 ? j : "0" + j);
			obj3[1] = list1;
			list.add(obj3);
		}
	}

	public String getWhere(Integer i, Integer j) {
		StringBuffer sb = new StringBuffer(" where 1=1 ");
		sb.append(" and a.op_time >= '" + i + "-" + j + "-01'");

		if (j != 12) {
			sb.append(" and a.op_time < '" + i + "-" + (j + 1) + "-01'");
		} else {
			sb.append(" and a.op_time < '" + (i + 1) + "-01-01'");
		}

		sb.append(" group by a.event_type order by zongji");

		return sb.toString();
	}
	
	public void doSearch1(List<Object[]> list, Integer i, String eventType) {
		StringBuilder sb = new StringBuilder();
		sb
		.append(" select")
		.append(" case when a.op_time >= '").append(i).append("-01-01' and a.op_time < '").append(i).append("-02-01' then '1'")
		.append(" when a.op_time >= '").append(i).append("-02-01' and a.op_time < '").append(i).append("-03-01' then '2'")
		.append(" when a.op_time >= '").append(i).append("-03-01' and a.op_time < '").append(i).append("-04-01' then '3'")
		.append(" when a.op_time >= '").append(i).append("-04-01' and a.op_time < '").append(i).append("-05-01' then '4'")
		.append(" when a.op_time >= '").append(i).append("-05-01' and a.op_time < '").append(i).append("-06-01' then '5'")
		.append(" when a.op_time >= '").append(i).append("-06-01' and a.op_time < '").append(i).append("-07-01' then '6'")
		.append(" when a.op_time >= '").append(i).append("-07-01' and a.op_time < '").append(i).append("-08-01' then '7'")
		.append(" when a.op_time >= '").append(i).append("-08-01' and a.op_time < '").append(i).append("-09-01' then '8'")
		.append(" when a.op_time >= '").append(i).append("-09-01' and a.op_time < '").append(i).append("-10-01' then '9'")
		.append(" when a.op_time >= '").append(i).append("-10-01' and a.op_time < '").append(i).append("-11-01' then '10'")
		.append(" when a.op_time >= '").append(i).append("-11-01' and a.op_time < '").append(i).append("-12-01' then '11'")
		.append(" when a.op_time >= '").append(i).append("-12-01' and a.op_time < '").append(i+1).append("-01-01' then '12'")
		.append(" else 'c' end,")
		.append(" count(*)")
		.append(" from s_log a")
		.append(" where a.op_time >= '").append(i).append("-01-01' and a.op_time < '")
		.append(i+1).append("-01-01' and a.event_type='").append(eventType).append("'")
		.append(" group by")
		.append(" case when a.op_time >= '").append(i).append("-01-01' and a.op_time < '").append(i).append("-02-01' then '1'")
		.append(" when a.op_time >= '").append(i).append("-02-01' and a.op_time < '").append(i).append("-03-01' then '2'")
		.append(" when a.op_time >= '").append(i).append("-03-01' and a.op_time < '").append(i).append("-04-01' then '3'")
		.append(" when a.op_time >= '").append(i).append("-04-01' and a.op_time < '").append(i).append("-05-01' then '4'")
		.append(" when a.op_time >= '").append(i).append("-05-01' and a.op_time < '").append(i).append("-06-01' then '5'")
		.append(" when a.op_time >= '").append(i).append("-06-01' and a.op_time < '").append(i).append("-07-01' then '6'")
		.append(" when a.op_time >= '").append(i).append("-07-01' and a.op_time < '").append(i).append("-08-01' then '7'")
		.append(" when a.op_time >= '").append(i).append("-08-01' and a.op_time < '").append(i).append("-09-01' then '8'")
		.append(" when a.op_time >= '").append(i).append("-09-01' and a.op_time < '").append(i).append("-10-01' then '9'")
		.append(" when a.op_time >= '").append(i).append("-10-01' and a.op_time < '").append(i).append("-11-01' then '10'")
		.append(" when a.op_time >= '").append(i).append("-11-01' and a.op_time < '").append(i).append("-12-01' then '11'")
		.append(" when a.op_time >= '").append(i).append("-12-01' and a.op_time < '").append(i+1).append("-01-01' then '12'")
		.append(" else 'c' end;");
		QueryCache qc = new QueryCache(sb.toString(), true);
		List<?> list1 = qc.listCache();
		if (list1 != null && list1.size() > 0) {
			Object[] obj3 = new Object[2];
			obj3[0] = "" + i;
			obj3[1] = list1;
			list.add(obj3);
		}
	}
	public void doSearch2(List<Object[]> list, Integer i, Integer j) {
		StringBuffer sb = new StringBuffer(
				" select a.event_type,"
				+ " count(*) as zongji "
				+ " from s_log a " + getWhere(i, j));
		QueryCache qc = new QueryCache(sb.toString(), true);
		List<?> list1 = qc.listCache();
		if (list1 != null && list1.size() > 0) {
			Object[] obj3 = new Object[2];
			obj3[0] = "" + i + "-" + (j > 9 ? j : "0" + j);
			obj3[1] = list1;
			list.add(obj3);
		}
	}
	
	public String getWhere2(Integer i, Integer j) {
		StringBuffer sb = new StringBuffer(" where 1=1 ");
		sb.append(" and a.op_time >= '" + i + "-" + j + "-01'");
		
		if (j != 12) {
			sb.append(" and a.op_time < '" + i + "-" + (j + 1) + "-01'");
		} else {
			sb.append(" and a.op_time < '" + (i + 1) + "-01-01'");
		}
		
		sb.append(" group by a.event_type");
		
		return sb.toString();
	}

	public String getStatBeginDate() {
		return statBeginDate;
	}

	public void setStatBeginDate(String statBeginDate) {
		this.statBeginDate = statBeginDate;
	}

	public String getEventType() {
		return eventType;
	}

	public void setEventType(String eventType) {
		this.eventType = eventType;
	}

	public String getList0s() {
		return list0s;
	}

	public void setList0s(String list0s) {
		this.list0s = list0s;
	}

	public String getList1s() {
		return list1s;
	}

	public void setList1s(String list1s) {
		this.list1s = list1s;
	}

	public String getList2s() {
		return list2s;
	}

	public void setList2s(String list2s) {
		this.list2s = list2s;
	}

	public String getList3s() {
		return list3s;
	}

	public void setList3s(String list3s) {
		this.list3s = list3s;
	}

	public String getList4s() {
		return list4s;
	}

	public void setList4s(String list4s) {
		this.list4s = list4s;
	}

	public String getList10s() {
		return list10s;
	}

	public void setList10s(String list10s) {
		this.list10s = list10s;
	}

	public String getList20s() {
		return list20s;
	}

	public void setList20s(String list20s) {
		this.list20s = list20s;
	}

	public String getList21s() {
		return list21s;
	}

	public void setList21s(String list21s) {
		this.list21s = list21s;
	}

}
